<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Admin\Form;

use Admin\Validator\LocationMemberExistsValidator;
use Laminas\Form\Form;

class SelfLocationMemberForm extends Form
{
    private $entityManager;
    private $member;
    private $type;

    public function __construct($entityManager, $member, $type = 'add', $name = 'self-location-member-form', $options = [])
    {
        parent::__construct($name, $options);
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');

        $this->entityManager= $entityManager;
        $this->member       = $member;
        $this->type         = $type;

        $this->addElements();
        $this->addInputFilter();
    }

    protected function addElements()
    {
        $this->add([
            'type'  => 'text',
            'name'  => 'memberAccount',
            'attributes'    => [
                'id'            => 'memberAccount',
                'class'         => 'form-control'
            ]
        ]);

        if($this->type == 'add') {
            $this->add([
                'type'  => 'password',
                'name'  => 'memberPassword',
                'attributes'    => [
                    'id'            => 'memberPassword',
                    'class'         => 'form-control'
                ]
            ]);

            $this->add([
                'type'  => 'password',
                'name'  => 'memberComPassword',
                'attributes'    => [
                    'id'            => 'memberComPassword',
                    'class'         => 'form-control'
                ]
            ]);
        }

        $this->add([
            'type'  => 'text',
            'name'  => 'memberName',
            'attributes'    => [
                'id'            => 'memberName',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'memberPhone',
            'attributes'    => [
                'id'            => 'memberPhone',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'select',
            'name'  => 'locationId',
            'attributes'    => [
                'id'            => 'locationId',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'memberState',
            'attributes'    => [
                'id'            => 'memberState',
                'value'         => 1
            ]
        ]);
    }

    protected function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        $inputFilter->add([
            'name'      => 'memberName',
            'required'  => true,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ],
            'validators'=> [
                [
                    'name'      => 'StringLength',
                    'options'   => [
                        'min'   => 1,
                        'max'   => 50
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'memberPhone',
            'required'  => true,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'locationId',
            'required'  => true,
            'filters'   => [
                ['name' => 'ToInt']
            ],
            'validators'=> [
                [
                    'name'      => 'GreaterThan',
                    'options'   => [
                        'min'   => 0
                    ]
                ]
            ]
        ]);

        if($this->type == 'add') {
            $inputFilter->add([
                'name'      => 'memberAccount',
                'required'  => true,
                'filters'   => [
                    ['name' => 'StringTrim'],
                    ['name' => 'StripTags'],
                    ['name' => 'HtmlEntities']
                ],
                'validators'=> [
                    [
                        'name'      => 'StringLength',
                        'options'   => [
                            'min'   => 1,
                            'max'   => 50
                        ]
                    ],
                    [
                        'name'      => LocationMemberExistsValidator::class,
                        'options'    => [
                            'entityManager' => $this->entityManager,
                            'member'        => $this->member
                        ]
                    ]
                ]
            ]);

            $inputFilter->add([
                'name'      => 'memberPassword',
                'required'  => true,
                'filters'   => [
                    ['name' => 'StringTrim']
                ],
                'validators'=> [
                    [
                        'name'      => 'StringLength',
                        'options'   => [
                            'min'   => 6
                        ]
                    ]
                ]
            ]);

            $inputFilter->add([
                'name'      => 'memberComPassword',
                'required'  => true,
                'filters'   => [
                    ['name' => 'StringTrim']
                ],
                'validators'=> [
                    [
                        'name'      => 'Identical',
                        'options'   => [
                            'token' => 'memberPassword'
                        ]
                    ]
                ]
            ]);
        }

        $inputFilter->add([
            'name'      => 'memberState',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);
    }
}